package com.gzz.orchid.context.domain.rbac.entity;

import com.gzz.orchid.common.core.tuple.DateTimeRange;
import com.gzz.orchid.context.infrastructure.definition.Action;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 特权
 *
 * @author higmd
 */
@Data
public class Privilege {
    /**
     * 模块编码
     */
    private String moduleCode;
    /**
     * 权限动作
     */
    private Set<Action> actions;
    /**
     * 权限有效期
     */
    private DateTimeRange validTime;

    public static Privilege of(String moduleCode, String[] actions, LocalDateTime begin, LocalDateTime end) {
        Privilege privilege = new Privilege();
        privilege.setModuleCode(moduleCode);
        privilege.setActions(Arrays.stream(actions).map(Action::of).collect(Collectors.toSet()));
        privilege.setValidTime(new DateTimeRange(begin, end));
        return privilege;
    }

    public static Privilege of(String moduleCode, Integer actions, LocalDateTime begin, LocalDateTime end) {
        Privilege privilege = new Privilege();
        privilege.setModuleCode(moduleCode);
        privilege.setActions(Action.parseSet(actions));
        privilege.setValidTime(new DateTimeRange(begin, end));
        return privilege;
    }
}
